using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Battle_GameOver : MonoBehaviour
{
	public static Battle_GameOver Instance;

	[Header("-- References --")]
	[SerializeField]
	private GameObject ObjectHolder;

	[SerializeField]
	private SpriteRenderer Soul;

	[Space(5f)]
	[SerializeField]
	private Sprite Soul_Regular;

	[SerializeField]
	private Sprite Soul_Break;

	[SerializeField]
	private ParticleSystem Soul_ShatterParticles;

	[SerializeField]
	private SpriteRenderer GameOverTitle;

	[Space(5f)]
	[SerializeField]
	private ChatboxSimpleText GameOverText;

	public CHATBOXTEXT[] PossibleDeathMessages;

	private int currentIndex;

	private CHATBOXTEXT targetText;

	[Header("-- Choices --")]
	[SerializeField]
	private TextMeshPro ContinueText;

	[Header("-- Choices --")]
	[SerializeField]
	private TextMeshPro GiveUpText;

	[SerializeField]
	private SpriteRenderer GhostSoul;

	private int CurrentSelectedChoice = -1;

	[Header("-- Sounds --")]
	[SerializeField]
	private AudioClip SoulSFX_Break;

	[SerializeField]
	private AudioClip SoulSFX_Shatter;

	[SerializeField]
	private AudioClip Music_FaintCourage;

	private void Awake()
	{
		Instance = this;
	}

	private void Start()
	{
		Music_FaintCourage.LoadAudioData();
		SceneManager.sceneLoaded += OnSceneLoaded;
	}

	private void ResetGameOver()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		GameOverTitle.color = Color.clear;
		ObjectHolder.SetActive(false);
		Soul.sprite = Soul_Regular;
		((Renderer)Soul).enabled = true;
		Soul_ShatterParticles.Stop();
		CurrentSelectedChoice = -1;
		((Graphic)ContinueText).color = Color.clear;
		((Graphic)GiveUpText).color = Color.clear;
		((Component)GhostSoul).transform.localPosition = Vector2.op_Implicit(new Vector2(0f, -3.65f));
		GhostSoul.color = Color.clear;
	}

	public void PlayGameOver(Vector2 SoulStartingPosition)
	{
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		MusicManager.StopSong(Fade: false, 0f);
		PlayerManager.Instance._PlayerState = PlayerManager.PlayerState.Cutscene;
		LightworldMenu.Instance.CanOpenMenu = false;
		DarkworldMenu.Instance.CanOpenMenu = false;
		GonerMenu.Instance.CanOpenGonerMenu = false;
		ObjectHolder.SetActive(true);
		((Component)Soul).transform.position = Vector2.op_Implicit(SoulStartingPosition);
		((MonoBehaviour)this).StartCoroutine(GameOver());
	}

	private IEnumerator GameOver()
	{
		yield return (object)new WaitForSeconds(0.5f);
		BattleSystem.PlayBattleSoundEffect(SoulSFX_Break);
		DEBUG_EnableMarkiplier.ChangeMarkiplierState(DEBUG_EnableMarkiplier.MarkiplierEmotions.Shock);
		Soul.sprite = Soul_Break;
		yield return (object)new WaitForSeconds(1.5f);
		((Renderer)Soul).enabled = false;
		Soul_ShatterParticles.Play();
		BattleSystem.Instance.CloseBattleBox();
		BattleSystem.PlayBattleSoundEffect(SoulSFX_Shatter);
		yield return (object)new WaitForSeconds(2.5f);
		DEBUG_EnableMarkiplier.ChangeMarkiplierState(DEBUG_EnableMarkiplier.MarkiplierEmotions.Annoyed);
		MusicManager.PlaySong(Music_FaintCourage, FadePreviousSong: false, 0f);
		GameOverTitle.color = new Color(1f, 1f, 1f, 0f);
		CutsceneUtils.FadeInSprite(GameOverTitle, 0.5f);
		yield return (object)new WaitForSeconds(1f);
		targetText = PossibleDeathMessages[Random.Range(0, PossibleDeathMessages.Length)];
		GameOverText.RunText(targetText, currentIndex);
	}

	public void ProgressGameoverDialogue()
	{
		((MonoBehaviour)this).StartCoroutine(ProgressDialogueDelay());
	}

	private IEnumerator ProgressDialogueDelay()
	{
		yield return (object)new WaitForSeconds(1.75f);
		GameOverText.CurrentTextIndex++;
		GameOverText.RunText(targetText, currentIndex);
	}

	public void EndGameoverDialogue()
	{
		((MonoBehaviour)this).StartCoroutine(EndDialogueDelay());
	}

	private IEnumerator EndDialogueDelay()
	{
		bool hasSelectedChoice = false;
		yield return (object)new WaitForSeconds(1.75f);
		GameOverText.EndText();
		((Graphic)ContinueText).color = new Color(1f, 1f, 1f, 0f);
		CutsceneUtils.FadeInText3D(ContinueText, 0.5f);
		((Graphic)GiveUpText).color = new Color(1f, 1f, 1f, 0f);
		CutsceneUtils.FadeInText3D(GiveUpText, 0.5f);
		GhostSoul.color = new Color(1f, 1f, 1f, 0f);
		CutsceneUtils.FadeInSprite(GhostSoul, 0.5f, 0.133f);
		yield return (object)new WaitForSeconds(0.5f);
		while (!hasSelectedChoice)
		{
			yield return null;
			Vector2 val = Vector2.op_Implicit(((Component)GhostSoul).transform.localPosition);
			if (CurrentSelectedChoice != 0 && CurrentSelectedChoice != 1)
			{
				((Vector2)(ref val))._002Ector(0f, -3.65f);
				((Graphic)ContinueText).color = Color.white;
				((Graphic)GiveUpText).color = Color.white;
			}
			else if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm))
			{
				hasSelectedChoice = true;
				continue;
			}
			if (CurrentSelectedChoice == 0)
			{
				((Vector2)(ref val))._002Ector(-3.55f, -3.65f);
				((Graphic)ContinueText).color = Color.yellow;
				((Graphic)GiveUpText).color = Color.white;
			}
			if (CurrentSelectedChoice == 1)
			{
				((Vector2)(ref val))._002Ector(3.55f, -3.65f);
				((Graphic)ContinueText).color = Color.white;
				((Graphic)GiveUpText).color = Color.yellow;
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Left))
			{
				CurrentSelectedChoice = 0;
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Right))
			{
				CurrentSelectedChoice = 1;
			}
			((Component)GhostSoul).transform.localPosition = Vector2.op_Implicit(Vector2.Lerp(Vector2.op_Implicit(((Component)GhostSoul).transform.localPosition), val, 4f * Time.deltaTime));
		}
		LightworldMenu.Instance.CanOpenMenu = true;
		DarkworldMenu.Instance.CanOpenMenu = true;
		PlayerManager.Instance._PlayerHealth = PlayerManager.Instance._PlayerMaxHealth;
		if (CurrentSelectedChoice == 0)
		{
			GonerMenu.RecoveringFromGameOver = true;
			UI_FADE instance = UI_FADE.Instance;
			Scene activeScene = SceneManager.GetActiveScene();
			instance.StartFadeIn(((Scene)(ref activeScene)).buildIndex, 0.5f);
		}
		else
		{
			UI_FADE.Instance.StartFadeIn(2, 0.5f, UnpauseOnEnd: false, NewMainMenuManager.MainMenuStates.Hypothetical);
		}
		GonerMenu.Instance.CanOpenGonerMenu = true;
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		Debug.Log((object)("Scene Loaded: " + ((Scene)(ref scene)).name));
		Debug.Log((object)"PlayerPrefs saved!");
		PlayerPrefs.Save();
		ResetGameOver();
	}

	private void OnDestroy()
	{
		SceneManager.sceneLoaded -= OnSceneLoaded;
	}
}
